package com.owen.springboot.smartparkingms.module.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.owen.springboot.smartparkingms.common.model.Result;
import com.owen.springboot.smartparkingms.common.util.ShiroUtils;
import com.owen.springboot.smartparkingms.module.app.entity.CarOrder;
import com.owen.springboot.smartparkingms.module.app.entity.Park;
import com.owen.springboot.smartparkingms.module.app.mapper.ParkMapper;
import com.owen.springboot.smartparkingms.module.app.service.IParkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.owen.springboot.smartparkingms.module.sys.entity.SysUser;
import com.owen.springboot.smartparkingms.module.sys.mapper.UserParkMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Owen
 * @since 2022-02-25
 */
@Service
public class ParkServiceImpl extends ServiceImpl<ParkMapper, Park> implements IParkService {

    @Resource
    ParkMapper parkMapper;

    @Resource
    UserParkMapper userParkMapper;

    @Override
    public List<Park> select() {
        return parkMapper.selectList(null);
    }

    @Override
    public Result listPage(Park park) {
        IPage<Park> page = new Page(park.getPageNo(),park.getPageSize());
        SysUser sysUser = ShiroUtils.getUserEntity();
        IPage<Park> list;
        if(sysUser.getUsername().equals("admin")){
            QueryWrapper query = new QueryWrapper<>()
                    .like("name",park.getDescription());
            list = parkMapper.selectPage(page,query);
        }else {
            List<Long> parkIdList = userParkMapper.selectparkId(sysUser.getUserId());
            QueryWrapper queryWrapper = new QueryWrapper<>()
                    .in("park_id",parkIdList)
                    .like("name",park.getDescription());
            list = parkMapper.selectPage(page,queryWrapper);
        }
        return Result.ok(list);
    }

    @Override
    public List<Park> selectByUserId(Long userId) {
        return parkMapper.selectByUserId(userId);

    }

}
